package org.bluedream.core.module.controller.clsItem;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bluedream.comm.base.BaseController;
import org.bluedream.comm.utils.excel.ExcelUtil;
import org.bluedream.core.module.entity.clsItem.ClsItem;
import org.bluedream.core.module.entity.goods.Goods;
import org.bluedream.core.module.service.clsItem.ClsItemService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ClsItemController
 * @Description TODO
 * @Author foxsand
 * @Data 2021/4/23 14:32
 * @Version
 */
@Controller
@RequestMapping(value = "${adminPath}/clsItem")
public class ClsItemController extends BaseController {
    @Resource
    private ClsItemService clsItemService;

    @ModelAttribute
    public ClsItem get(String id){
        return clsItemService.getById(id);
    }

    @RequiresPermissions("clsItem:view")
    @RequestMapping(value = {"/list" , ""})
    public String list(ClsItem clsItem, Model model){
        model.addAttribute("clsItem" , clsItem);
        return "module/clsItem/clsItemList";
    }

    @RequiresPermissions("clsItem:view")
    @RequestMapping("/form")
    public String form(ClsItem clsItem, Model model){
        clsItem = clsItemService.createNextNode(clsItem , "code" , "tree_sorts" , "tree_sort");
        model.addAttribute("clsItem" , clsItem);
        return "module/clsItem/clsItemForm";
    }

    @RequiresPermissions("clsItem:view")
    @RequestMapping("/findPage")
    @ResponseBody
    public IPage<ClsItem> findPage(ClsItem clsItem){
        return clsItemService.findPage(clsItem);
    }

    @RequiresPermissions("clsItem:view")
    @RequestMapping("/treeData")
    @ResponseBody
    public List<Map<String , Object>> treeData(@RequestParam(defaultValue="1")String isShowCode , @RequestParam(defaultValue="undefined")String excludeCode){
        return clsItemService.treeData(excludeCode , isShowCode);
    }

    @RequiresPermissions("clsItem:view")
    @RequestMapping("/treeDataToForm")
    @ResponseBody
    public List<Map<String , Object>> treeDataToForm(@RequestParam(defaultValue="1")String isShowCode , @RequestParam(defaultValue="undefined")String excludeCode){
        return clsItemService.treeData(excludeCode , isShowCode);
    }

    /**
     * 创建下一节点
     * @param clsItem
     * @return
     */
    @RequiresPermissions("clsItem:edit")
    @RequestMapping(value = "/createNextNodeToForm" , method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ClsItem createNextNodeToForm(@RequestBody ClsItem clsItem){
        return clsItemService.createNextNode(clsItem , "code" , "tree_sorts" , "tree_sort");
    }

    @RequiresPermissions("clsItem:edit")
    @RequestMapping("/save")
    @ResponseBody
    public String save(ClsItem clsItem){
        boolean flag = clsItemService.save(clsItem);
        if (flag){
            return "品类信息保存成功";
        }
        return "品类信息保存失败";
    }

    /**
     * 导出excel演示
     * @param request
     * @param resp
     */
    @RequiresPermissions("goods:view")
    @RequestMapping(value = "/fileDownload" , method = RequestMethod.GET)
    public void fileDownload(HttpServletRequest request, HttpServletResponse resp){
        try {
            List<ClsItem> list = new ArrayList<>();
            ExcelUtil.export4List(resp , list , Goods.class , "exportFileName" , "exportSheetName" , "测试Excel文件导出");
        }catch (Exception e){
            e.getMessage();
        }
    }

    /**
     * 导入excel演示
     * @param file
     */
    @RequiresPermissions("clsItem:edit")
    @RequestMapping(value = "/excelImport")
    @ResponseBody
    public void excelImport(@RequestParam("file") MultipartFile file){
        List<ClsItem> list = ExcelUtil.import2List(file , 0 , ClsItem.class , 0);
        clsItemService.excelImport(list);
    }
}
